﻿using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common.CommonWordHandle;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LdtJudge.DataEngine.Common
{

    /// <summary>
    /// 固定业务的数据处理
    /// </summary>
    public static class BllDataHandle
    {
        /// <summary>
        /// 程序枚举类型
        /// </summary>
        public enum PathType
        {
            Server = 1,
            From = 2,
            Web = 3
        }
        /// <summary>
        /// 远程调用类型
        /// </summary>
        public enum RemoteConfigSysType
        {
            /// <summary>
            /// 应用程序配置
            /// </summary>
            WordParse,
            /// <summary>
            /// 系统库配置
            /// </summary>
            SysJudge,
        }
        /// <summary>
        /// 程序类型
        /// </summary>
        public static PathType Pathtype { get; set; }
        /// <summary>
        /// 根据案件类型获取案件的类型编号
        /// Returns the type of the case.
        /// </summary>
        /// <param name="caseType">Type of the case.</param>
        /// <returns></returns>
        public static string ReturnCaseType(string caseType)
        {
            switch (caseType.ToLower().Trim())
            {
                case "a1":
                case "xs":
                case "a2":
                case "a3":
                    return "1000";
                case "b1":
                case "b2":
                case "b3":
                case "ms":
                    return "1477";
                case "c1":
                case "xz":
                case "c2":
                case "c3":
                    return "2329";
                case "zx":
                    return "2449";
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取当前程序的运行路径，主要用于判断程序的调用是
        /// window  还是 web
        /// </summary>
        /// <returns></returns>
        public static string GetCurrentProgramPath()
        {
            //1、Server.MapPath
            //2、System.Windows.Forms.StartupPath
            //3、Type.Assembly.Location
            string path = "";
            //// 获取程序的基目录。
            //string aa = System.AppDomain.CurrentDomain.BaseDirectory;
            //// 获取模块的完整路径。
            //string bb = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            //// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
            //string cc = System.Environment.CurrentDirectory;
            //// 获取应用程序的当前工作目录。
            //string dd = System.IO.Directory.GetCurrentDirectory();
            //// 获取和设置包括该应用程序的目录的名称。
            //string ee = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            if (Pathtype == 0)
            {
                Pathtype = (PathType)1;
            }

            if (Pathtype == PathType.Server || Pathtype == PathType.From)
            {
                path = AppDomain.CurrentDomain.BaseDirectory;
            }
            else if (Pathtype == PathType.Web)
            {
                path = AppDomain.CurrentDomain.BaseDirectory + @"\\Bin\\";
            }


            return path;
        }

        /// <summary>
        /// 获取doc文件流的内容
        /// </summary>
        /// <param name="filestream">文件流</param>
        /// <returns></returns>
        public static string GetDocText(byte[] filestream)
        {
            string strcontent = "";
            WordHandle wordHandle = new WordHandle();
            strcontent = wordHandle.GetWordContent(filestream);
            return strcontent;
        }
        /// <summary>
        /// 将文件字节流转换存储到docx文件中
        /// </summary>
        /// <param name="filestream">字节流</param>
        /// <param name="filePath">文件路径</param>
        /// <param name="fileName">文件名称</param>
        public static void SaveDocFile(byte[] filestream, string filePath, string fileName)
        {
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            using (System.IO.Stream stream = new System.IO.MemoryStream(filestream))
            {
                Aspose.Words.Document doc = new Aspose.Words.Document(stream);
                doc.Save(fileName + fileName, Aspose.Words.SaveFormat.Docx);
            }
        }

        /// <summary>
        /// 先进行拆分path，然后获取对应的内容
        /// </summary>
        /// <param name="filepath">filepath</param>
        /// <param name="configSysType">远程调用类型</param>
        /// <returns></returns>
        public static string SplitPathThenGetConfigContent(string filepath, RemoteConfigSysType configSysType = RemoteConfigSysType.WordParse)
        {
            string filetype = Path.GetExtension(filepath).TrimStart('.');
            string fileName = Path.GetFileNameWithoutExtension(filepath);
            string moudlepath = Path.GetDirectoryName(filepath);
            return GetConfigContext(configSysType, moudlepath, fileName, filetype);
        }

        /// <summary>
        /// 根据模块及文件名称获取对应文件的内容
        /// </summary>
        /// <param name="remoteConfig">远程调用类型</param>
        /// <param name="modelPath">模块所在路径</param>
        /// <param name="fileName">文件名称</param>
        /// <param name="fileType">文件类型</param>
        /// <returns>获取文件内容</returns>
        public static string GetConfigContext(RemoteConfigSysType remoteConfig, string modelPath, string fileName, string fileType)
        {
            //表示调用远程连接
            string remotetype = "";
            switch (remoteConfig)
            {
                case RemoteConfigSysType.WordParse:
                    remotetype = "wordparse2.0";
                    break;
                case RemoteConfigSysType.SysJudge:
                    remotetype = "ldt_judgedocsystem";
                    break;
                default:
                    break;
            }
            try
            {
                return ConfigRetrieval.GetConfigRetrieval.GetRemoteFileContent(remotetype, modelPath, fileName, fileType);
            }
            catch (Exception ex)
            {
               

            }
            return "";

        }
    }
}
